package com.warmlight.voicepacket.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.MBaseAdapter;
import com.warmlight.voicepacket.data.RecommendData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jason on 2018/5/30.
 */

public class RecommendHeaderAdapter extends MBaseAdapter {

    public RecommendHeaderAdapter(Context context, List<RecommendData.DataBean.PlayCoversBean> list) {
        super(context, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(mContext, R.layout.item_recommend_header, null);
            holder = new ViewHolder();
            holder.iv_header_bg = (ImageView) convertView.findViewById(R.id.iv_header_bg);
            holder.tv_header_name = (TextView) convertView.findViewById(R.id.tv_header_name);
            convertView.setTag(holder);
        }

        String url = ((RecommendData.DataBean.PlayCoversBean)(mList.get(position))).getPic();
        RequestOptions options = new RequestOptions();
        options.dontAnimate();
        Glide.with(mContext).load(TextUtils.isEmpty(url) ? R.drawable.ic_launcher_foreground : url).apply(options).into(holder.iv_header_bg);
        holder.tv_header_name.setText(((RecommendData.DataBean.PlayCoversBean)mList.get(position)).getName());

        return convertView;
    }

    private class ViewHolder {
        ImageView iv_header_bg;
        TextView tv_header_name;
    }

    public void addBefore(List<RecommendData.DataBean.PlayCoversBean> list){
        if(mList == null){
            mList = new ArrayList<>();
        }
        mList.addAll(0,list);
        notifyDataSetChanged();
    }
}
